import {ServerHost} from './common'
import Taro from "@tarojs/taro";
// import wx from '_weixin-js-sdk'
// let wx = require('weixin-js-sdk');

export const getJsApiParam = () => {
  let currentUrl = window.location.href
  let apiUrl = `${ServerHost}/api/Account/getWxJsApiParams?curUrl=${currentUrl}`
  Taro.showLoading({title: '加载中...'})
  fetch(apiUrl, {
    method: 'GET',
    mode: "cors",
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
    credentials: 'same-origin'
  })
    .then(response => {
      Taro.hideLoading()
      return response.json()
    })
    .then(response => {
      let appId = response.appId
      let timestamp = response.timestamp
      let nonceStr = response.nonceStr
      let signature = response.signature
      wx.config({
        debug: false,
        appId,
        timestamp,
        nonceStr,
        signature,
        jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage']
      });
    })
    .catch(error => {
      console.log(error)
    })
}

export const WxShare = async (url, img, name, desc) => {
    wx.ready(function () {
      wx.onMenuShareAppMessage({
        title: name,
        desc,
        link: url,
        imgUrl: img,
      });
      wx.onMenuShareTimeline({
        title: name, // 分享标题
        link: url1, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: img, // 分享图标
      })
    });
    wx.error(function (res) {
      console.log('微信接口校验失败')
    });
}

